import numpy as np  
import pandas as pd  
import tensorflow as tf  
from sklearn.model_selection import train_test_split  
  
# 假设已经准备好带钢产品的规格数据、工艺参数和硬度数据  
# 请根据实际情况导入数据  
X = pd.read_csv('data.csv', usecols=[2, 6, 7, 10])  # 选取四个特征值：均热炉温度、碳含量、快冷炉温度、加热炉温度  
y = pd.read_csv('data.csv', usecols=[12])  # 硬度数据  
  
# 划分数据集为训练集和测试集  
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)  
  
# 由于CNN需要三维输入，我们添加一个额外的维度  
X_train = X_train.values.reshape((X_train.shape[0], X_train.shape[1], 1))  
X_test = X_test.values.reshape((X_test.shape[0], X_test.shape[1], 1))  
  
# 构建卷积神经网络模型  
model = tf.keras.Sequential([  
    tf.keras.layers.Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(4, 1)),  
    tf.keras.layers.MaxPooling1D(pool_size=2),  
    tf.keras.layers.Flatten(),  
    tf.keras.layers.Dense(64, activation='relu'),  
    tf.keras.layers.Dense(1)  # 输出层，输出一个值作为硬度预测结果  
])  
  
# 编译模型  
model.compile(optimizer='adam', loss='mean_squared_error')  
  
# 训练模型  
model.fit(X_train, y_train, epochs=100, batch_size=32, validation_data=(X_test, y_test))  
  
# 评估模型性能  
loss = model.evaluate(X_test, y_test)  
print("模型在测试集上的损失:", loss)